package cheme.ProcessEditor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

public class ChemeFileInput implements IEditorInput
{
	Path path = null;
	File file = null;
	private boolean needsInitialSaveAs = false;
	
	public ChemeFileInput(ChemeFileInput input)
	{
		path = input.path;
		file = input.file;
		needsInitialSaveAs = input.needsInitialSaveAs;
	}
	
	public ChemeFileInput(String pathName)
	{
		setPath(pathName);
	}
	
	private void setNeedsInitialSaveAs(boolean needsInitialSaveAs)
	{
		this.needsInitialSaveAs = needsInitialSaveAs;
	}

	public boolean getNeedsInitialSaveAs()
	{
		return needsInitialSaveAs;
	}
	
	public Path getPath()
	{
		return path;
	}
	
	public void setPath(String pathName)
	{
		path = new Path(pathName);
	}
	
	public File getFile()
	{
		if(null == file)
		{
			file = getPath().toFile();
		}
		
		return file;
	}
	
	public InputStream getInputStream()
	{
		InputStream stream = null;
		
		if(exists())
		{
			/*
			 * IF the file exists, we can read data from that.
			 */
			
			try
			{
				stream = new FileInputStream(getPath().toString());
			}
			catch(FileNotFoundException e)
			{
				e.printStackTrace();
			}
		}
		else
		{
			setNeedsInitialSaveAs(true);
		}
		
		return stream;
	}
	
	public OutputStream getOutputStream()
	{
		OutputStream stream = null;
		
		try
		{
			stream = new FileOutputStream(getPath().toString());
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
		}
		
		return stream;
	}

	public boolean exists()
	{
		return getFile().exists();
	}

	public String getName()
	{
		return getPath().lastSegment();
	}

	public ImageDescriptor getImageDescriptor()
	{
		return null;
	}

	public IPersistableElement getPersistable()
	{
		return null;
	}

	public String getToolTipText()
	{
		return getName();
	}

	public Object getAdapter(Class adapter)
	{
		return null;
	}
	
	public void writeToFile(Object model)
	{
		try
		{
			OutputStream stream = getOutputStream();
			writeToOutputStream(stream, model);
			stream.close();
			
			setNeedsInitialSaveAs(false);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public Object readFromFile()
	{
		Object model = null;
		InputStream is = getInputStream();
		
		if(null != is)
		{
			model = readFromInputStream(is);
		}
		
		return model;
	}
	
	private void writeToOutputStream(OutputStream os, Object model) throws IOException
	{
		ObjectOutputStream out = new ObjectOutputStream(os);
		out.writeObject(model);
		out.close();
	}
	
	private Object readFromInputStream(InputStream is)
	{
		Object result = null;
		
		try
		{
			ObjectInputStream in = new ObjectInputStream(is);
			result = in.readObject();
			in.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
}
